package bidsanity



import grails.test.mixin.*
import org.junit.*
import grails.converters.JSON

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
 */
@TestFor(BidController)
@Mock([Customer, Bid, Listing])
class BidControllerTests {
    
    void testCreate(){
        def cust = controller.create()
        Assert.assertNotNull(cust)
    }

    void testSuccessfulSave() {
        controller.bidService = new BidService()
        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save()
        def b = new Customer(emailAddress: 'bidder@example.org', password: "testing").save()
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s).save()
        Map myParams = [listingId: l.id, amount: '2.0']  //this 2.0 needs to be a string

        controller.session.user = b.emailAddress
        controller.params.putAll(myParams)
        controller.save()
        def jsonObj = controller.response.json
        def status = jsonObj.status
        def message = jsonObj.message
        Assert.assertTrue(status)
        Assert.assertTrue(message == 'bid.was.registered')
    }

    void testFailedBidAmount() {
        controller.bidService = new BidService()
        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save()
        def b = new Customer(emailAddress: 'bidder@example.org', password: "testing").save()
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s).save()
        Map myParams = ['listing': l]  //for some reason this 2.0 needs to be a string
        controller.session.user = b.emailAddress
        controller.params.putAll(myParams)
        controller.save()
        def jsonObj = controller.response.json
        def status = jsonObj.status
        def message = jsonObj.message
        Assert.assertFalse(status)
        Assert.assertTrue(message == 'bid.was.not.valid')
    }
}
